package com.eshin.thymeleaf.controller;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;

import com.eshin.thymeleaf.seed.DateFormatter;
import com.eshin.thymeleaf.seed.VarietyFormatter;
import com.eshin.thymeleaf.seed.VarietyService;

/**
 * 
 * @author Eshin
 * spring集成thymeleaf官方文档中文翻译贴
 * ：http://www.cnblogs.com/jiangchao226/p/5937458.html
 */
@Configuration
public class ConfigBean {
	@Autowired
	VarietyService varietyService;
	
	
	@Bean(name="messageSource")
	public ResourceBundleMessageSource messageSource() {
	    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
	    
	    /**
	     * 国际化资源文件前缀resource_zh_CN.properties resource_es.properties
	     * 自动从classpath下加载
	     */
	    messageSource.setBasename("Messages");
	    return messageSource;
	}
	
	 @Bean
	    public SpringResourceTemplateResolver templateResolver(){
	        // SpringResourceTemplateResolver automatically integrates with Spring's own
	        // resource resolution infrastructure, which is highly recommended.
	        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
//	        templateResolver.setApplicationContext();
	        templateResolver.setPrefix("/WEB-INF/views/");
	        templateResolver.setSuffix(".html");
	        // HTML is the default value, added here for the sake of clarity.
	        templateResolver.setTemplateMode(TemplateMode.HTML);
	        // Template cache is true by default. Set to false if you want
	        // templates to be automatically updated when modified.
	        templateResolver.setCacheable(false);
//	                      读取模板时使用的编码格式
	        templateResolver.setCharacterEncoding("UTF-8");
	        return templateResolver;
	    }

	    @Bean
	    public SpringTemplateEngine templateEngine(){
	        // SpringTemplateEngine automatically applies SpringStandardDialect and
	        // enables Spring's own MessageSource message resolution mechanisms.
	        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
	        templateEngine.setTemplateResolver(templateResolver());
	        // Enabling the SpringEL compiler with Spring 4.2.4 or newer can
	        // speed up execution in most scenarios, but might be incompatible
	        // with specific cases when expressions in one template are reused
	        // across different data types, so this flag is "false" by default
	        // for safer backwards compatibility.
	        templateEngine.setEnableSpringELCompiler(true);
	        return templateEngine;
	    }

	    @Bean
	    public ThymeleafViewResolver viewResolver(){
	        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
	        viewResolver.setTemplateEngine(templateEngine());
//          视图解析器response时使用的编码格式
	        viewResolver.setCharacterEncoding("UTF-8");
	        // NOTE 'order' and 'viewNames' are optional
//	        viewResolver.setOrder(1);
//	                    此处设置之后suffix将失效
//	        viewResolver.setViewNames(new String[] {".html", ".xhtml"});
	        return viewResolver;
	    }
	    
	  /*  @Bean
	    public VarietyFormatter varietyFormatter() {
	        return new VarietyFormatter();
	    }

	    @Bean
	    public DateFormatter dateFormatter() {
	        return new DateFormatter();
	    }*/
	    
	    @Bean(name="conversionService")
	    public FormattingConversionServiceFactoryBean conversionService(ResourceBundleMessageSource messageSource){
	    	FormattingConversionServiceFactoryBean conversionService = new FormattingConversionServiceFactoryBean();
	    	Set<Object> formatters = new HashSet<>();
	    	formatters.add(new VarietyFormatter(varietyService));
	    	formatters.add(new DateFormatter(messageSource));
	    	conversionService.setFormatters(formatters);
	    	return conversionService;
	    }
}
